package com.wan37.client.message.received;

import com.google.protobuf.Message;
import com.googlecode.protobuf.format.JsonFormat;
import com.wan37.client.handler.ClientHandler;
import com.wan37.client.service.cache.UserServiceCache;
import com.wan37.protobuf.constant.CommonTask;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.GameTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author : luoyong
 * @date : 2020-08-10 09:44
 **/
@Component(PtoNum.TASKRESPONSE + "")
public class TaskResponse implements ReceiveMsg {
    private final Logger logger = LoggerFactory.getLogger(TaskResponse.class);
    @Autowired
    private UserServiceCache userServiceCache;
    private ClientHandler clientHandler = ClientHandler.getInstance();
    @Override
    public void execute(Message msg) {
        GameTask.TaskResponse taskResponse = (GameTask.TaskResponse) msg;
        int code = taskResponse.getCode();
        UserServiceCache.UserIn userIn = userServiceCache.getUserInMap().get(clientHandler.selfId);
        switch (code){
            case CommonTask.GET_ALL_TASK:
               for (GameTask.Task task : taskResponse.getTaskList()){
                   userIn.getTaskSonType2Task().put(task.getTaskSonType(), task);
               }
                logger.info("获取到当前用户的成就任务，可以选择对应的操作查看。");
                break;
           case CommonTask.COMPLETION_TASK:
               logger.info("恭喜您！完成了下面这个成就：");
               for (GameTask.Task task : taskResponse.getTaskList()){
                   logger.info(JsonFormat.printToString(userIn.getTaskSonType2Task().get(task.getTaskSonType())));
                   userIn.getTaskSonType2Task().put(task.getTaskSonType(), task);
               }
               logger.info("奖励已经发放，请注意查看！");
               break;
            case CommonTask.UPDATE_TASK:
                for (GameTask.Task task : taskResponse.getTaskList()){
                    userIn.getTaskSonType2Task().put(task.getTaskSonType(), task);
                }
                break;
            default:
                break;
        }
    }
}
